package com.yhy.wxzn.wcs.device.plc.bean;

import com.yhy.wxzn.common.utils.SpringUtil;
import lombok.Data;
import org.jeecg.common.util.RedisUtil;

@Data
public class HoistPlc extends BasePlc {
    /**
     * 提升机编号
     */
    private volatile String hoistId;
    /**
     * 任务号
     */
    private volatile int taskId;
    /**
     * 提升机当前层
     */
    private volatile int floor;
    /**
     * 状态：1停机未启动 2联机自动 3脱机自动 4手动 5故障 6维修
     */
    private volatile int autoStatus;
    /**
     * 工作状态：0就绪 1放货中 2升降中
     */
    private volatile int workStatus;

    public static String parseWorkStatus(int status){
        if (status == 1) return "就绪";
        if (status == 2) return "放货中";
        if (status == 3) return "升降中";
        return "未知";
    }
    /**
     * 是否有货：0无货 1有货
     */
    private volatile boolean haveStock;
    /**
     * 是否有小车：0无车 1有车
     */
    private volatile boolean haveCar;
    /**
     * 是否准出入小车：0不允许 1允许
     */
    private volatile boolean accessCar;
    /**
     * 当前层死档信号：0缩回 1伸出
     */
    //private boolean blocker;

//    public static String parseBlocker(boolean status){
//        if (status) return "伸出";
//        return "缩回";
//    }
    /**
     * 故障码
     */
    private volatile int faultCode;
    public static String parseFaultCode(int faultCode){
        if (faultCode == 1) return "上升超程故障";
        if (faultCode == 2) return "下降超程故障";
        if (faultCode == 3) return "链条断裂故障";
        return "未知";
    }
    /**
     * 备用
     */
    private volatile int backup;
    /**
     * 是否锁定：0-未锁定 1-已锁定
     */
    private volatile int locking;

    //--------------------------------------------提升机写信息--------------------------------------------------------
    private volatile String writeAddress;
    //起始层
    private volatile int sourceFloor;
    //起始工位
    private volatile int sourceDevice;
    //目标层
    private volatile int targetFloor;
    //目标工位
    private volatile int targetDevice;
    //任务类型
    private volatile int taskType;
    //任务号
    private volatile int taskNo;
//    //小车入提升机完成
//    private boolean carInFinish;
//    //小车出提升机完成
//    private boolean carOutFinish;


    private static final String LIFTER_TASK_NO = "LIFTER_TASK_NO";

    public synchronized static int getAutoIncrementLiferTaskNo(){
        RedisUtil redisUtil = SpringUtil.getBean(RedisUtil.class);
        if (redisUtil.get(LIFTER_TASK_NO) == null || (int)redisUtil.get(LIFTER_TASK_NO) == 99999){
            redisUtil.set(LIFTER_TASK_NO,1);
            return 1;
        }
        return (int) redisUtil.incr(LIFTER_TASK_NO, 1);
    }
}
